package com.chai.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.chai.constant.MessageConstant;
import com.chai.pojo.OrderSetting;
import com.chai.service.OrderSettingService;
import com.chai.utils.POIUtils;
import com.chai.vo.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.ws.rs.POST;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/OrderSetting")
public class OrderSettingController {

    @Reference
    OrderSettingService orderSettingService;


    @PostMapping("/upload")
    public Result upload(@RequestBody MultipartFile excelFile){
        try {
            List<String[]> arrayList = POIUtils.readExcel(excelFile);
            List<OrderSetting> datalist = new ArrayList<OrderSetting>();
            for (String[] arrs : arrayList) {
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setOrderDate(new Date(arrs[0]));
                orderSetting.setNumber(Integer.parseInt(arrs[1]));
                orderSetting.setReservations(0);
                datalist.add(orderSetting);
            }
            orderSettingService.addBatch(datalist);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_FAIL);

        }
    }


    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date){
        try {
            List<Map<String, Integer>> list = orderSettingService.getOrderSettingByMonth(date);
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody OrderSetting orderSetting){
        try {
            orderSettingService.saveOrUpdate(orderSetting);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.ORDERSETTING_FAIL);

        }
    }
}
